import {defineStore} from 'pinia'
import {dbdto} from "../../wailsjs/go/models";
import {reactive} from "vue";
import {GetQuickTools} from "../../wailsjs/go/dbservice/UserToolService";
import {
    WindowGetPosition,
    WindowGetSize,
    WindowSetAlwaysOnTop,
    WindowSetTitle,
    WindowUnminimise
} from "../../wailsjs/runtime";
import {MainWinTitle} from "../constant/common";
import {MessageWinTitle, QuickWinTitle} from "../constant/winInfo";
import {
    ConfigKeyWinHeightMaine,
    ConfigKeyWinHeightMessage,
    ConfigKeyWinHeightQuick,
    ConfigKeyWinPosXMain,
    ConfigKeyWinPosXMessage,
    ConfigKeyWinPosYMain,
    ConfigKeyWinPosYMessage,
    ConfigKeyWinWidthMain,
    ConfigKeyWinWidthMessage,
    ConfigKeyWinWidthQuick
} from "../constant/config_keys";
import {SetKVT} from "../../wailsjs/go/dbservice/DbConfigService";
import {SettingTypeSystem} from "../constant/enums/setting_type";
import {ToastInfo} from "../utils/Toast";


export enum WindowsType {
    Main,
    Message,
    QuickTools ,
}
export enum ShowPopType {
    Login,
    UserInfo,
}

export const useWindowsStore =defineStore({
    id: "windows",
    state: () => ({
        CurrWindow:WindowsType.Main,
        Main:{
            FirstPos:{
                x:0,
                y:0
            },
            ShowPop:false,
            ShowPopTitle:"登录/注册",
            ShowPopWidth:"w-80",
            ShowPopType:ShowPopType.Login
        },
        Message:{
            IsComplete:true,//消息流已经结束
            Content:"",
            UseTime:0,
            OriginContent:"",
            ToolInfo:reactive<dbdto.UserToolInfo>(new dbdto.UserToolInfo()),
            RecStreamContent:"",//当前收到的流文本
            InputValues:reactive<Array<string>>([]),
            // PromptTemplate:"",
        },
        AppList:{
            //列表相关的
            Tools: reactive<dbdto.UserToolInfo[]>([])
        }
    }),
    actions: {
        GetToolList(){
            GetQuickTools().then(list=>{
                this.AppList.Tools=list
            })
        },
        //切换窗体
        ChangeWindow(Typ:WindowsType){
            this.CurrWindow=Typ
            switch (Typ) {
                case WindowsType.Main:
                    WindowSetTitle(MainWinTitle)
                    break
                case WindowsType.QuickTools:
                    WindowSetTitle(QuickWinTitle)
                    WindowUnminimise()
                    WindowSetAlwaysOnTop(true)
                    break
                case WindowsType.Message:
                    WindowSetTitle(MessageWinTitle)
                    WindowUnminimise()
                    WindowSetAlwaysOnTop(true)
                    break
            }

          //  this.SaveCurrWinSize(Typ)
        },
        async SaveCurrWinSize(winType: WindowsType) {
             console.log(winType," SaveCurrWinSize---winType")
            let size = await WindowGetSize()
            let pos = await WindowGetPosition()
            let key_size_w=ConfigKeyWinWidthMain
            let key_size_h=ConfigKeyWinHeightMaine

            let key_pox_x=ConfigKeyWinPosXMain
            let key_pox_y=ConfigKeyWinPosYMain

            if(winType == WindowsType.Message){
                 key_size_w=ConfigKeyWinWidthMessage
                 key_size_h=ConfigKeyWinHeightMessage
                 key_pox_x=ConfigKeyWinPosXMessage
                 key_pox_y=ConfigKeyWinPosYMessage
            }

            if(winType == WindowsType.QuickTools){
                 key_size_w=ConfigKeyWinWidthQuick
                 key_size_h=ConfigKeyWinHeightQuick
            }

             console.log(key_size_w,key_size_h," ---key_size_w,key_size_h")
            SetKVT(key_size_w,String(size.w),SettingTypeSystem)
            SetKVT(key_size_h,String(size.h),SettingTypeSystem)

            if(winType == WindowsType.Message ||winType == WindowsType.Main){
                //保存位置信息
                SetKVT(key_pox_x,String(pos.x),SettingTypeSystem)
                SetKVT(key_pox_y,String(pos.y),SettingTypeSystem)
            }

            if(winType == WindowsType.Message){
                ToastInfo("已记住【消息面板界面】尺寸和位置")
            }else if(winType == WindowsType.QuickTools){
                ToastInfo("已记住【快捷应用界面】尺寸")
            }else if(winType == WindowsType.Main){
                ToastInfo("已记住【主界面】尺寸和位置")
            }



        },
        MainShowPop(typ :ShowPopType,title:string,width:string){
            this.Main.ShowPop=true
            this.Main.ShowPopTitle=title
            this.Main.ShowPopWidth=width
            this.Main.ShowPopType=typ
        },
        CloseMainPop(){
            this.Main.ShowPop=false
        },
        //保存主界面初始坐标,不然会回不去了(仅启动时执行)
       InitMainFirstPos(x:number,y:number){
            if(this.Main.FirstPos.x==0 && this.Main.FirstPos.y == 0){
                this.Main.FirstPos.x=x
                this.Main.FirstPos.y=y
            }
        }
    },
    getters: undefined,
})